%%% iplayer.erl - helpers for getting stream information from
%%%    http://www.bbc.co.uk/iplayer
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2008 hellomatty@gmail.com
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%

-module(iplayer).

-import(web_helper).
-include_lib("util.hrl").

-export([version_for_episode/1, stream_for_version/1]).

-define(XPATH_GET_VERSION,"/playlist/item/@identifier").
-define(XPATH_GET_STREAM_ID, "/mediaSelection/media[@type='audio/mpeg']/connection/@identifier").
-define(XPATH_GET_STREAM_HOST, "/mediaSelection/media[@type='audio/mpeg']/connection/@server").
-define(XPATH_GET_STREAM_AUTH, "/mediaSelection/media[@type='audio/mpeg']/connection/@authString").
-define(STR_SET_PID, "iplayer.episode.setPidData(\"").

version_for_episode(Pid) ->
  case web_helper:parse_xml_uri(playlist_uri(Pid)) of
    {error, Error} ->
      ?D({iplayer,version_for_episode,get_failed,Error}),
      exit(failed_to_get_playlist);
    {ok, PlaylistXml} ->
      case web_helper:value( web_helper:apply_xpath(?XPATH_GET_VERSION, PlaylistXml) ) of
        [] ->
          ?D({iplayer,version_for_episode,no_version_found,episode,Pid}),
          exit(no_version_found);
        VersionPid ->
          VersionPid
      end
  end.

playlist_uri(Pid) ->
  "http://www.bbc.co.uk/iplayer/playlist/" ++ Pid.

stream_for_version(Pid) ->
  case web_helper:parse_xml_uri(mediasselector_uri(Pid)) of
    {ok, Xml} ->
      Host = case web_helper:value( web_helper:apply_xpath(?XPATH_GET_STREAM_HOST, Xml)) of
        [] ->
          ?D({iplayer,mediaselector,Xml}),
          exit(mediaselector_failed);
        H ->
          H
      end,
      [Type, StreamId] = string:tokens(web_helper:value( web_helper:apply_xpath(?XPATH_GET_STREAM_ID, Xml) ), ":"),
      Auth = web_helper:value( web_helper:apply_xpath(?XPATH_GET_STREAM_AUTH, Xml) ),
      {stream, Host, {Type, StreamId}, Auth};
    {error, Error} ->
      {error, Error}
  end.

mediasselector_uri(Pid) ->
  "http://www.bbc.co.uk/mediaselector/4/mtis/stream/" ++ Pid.
